home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 19 advanced win32 techniques / win32techniquesdemo / filesystemwatcherform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  8.7 KB  |  217 lines

  1. Imports System.IO
  2.  
  3. Public Class FileSystemWatcherForm
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.     Friend WithEvents fsw As System.IO.FileSystemWatcher
  28.     Friend WithEvents Label1 As System.Windows.Forms.Label
  29.                         Friend WithEvents txtPath As System.Windows.Forms.TextBox
  30.         Friend WithEvents Label2 As System.Windows.Forms.Label
  31.     Friend WithEvents chkIncludeSubdirs As System.Windows.Forms.CheckBox
  32.     Friend WithEvents txtLog As System.Windows.Forms.TextBox
  33.     Friend WithEvents chkEnableEvents As System.Windows.Forms.CheckBox
  34.     Friend WithEvents txtFilter As System.Windows.Forms.TextBox
  35.     Friend WithEvents btnWaitForChanged As System.Windows.Forms.Button
  36.     
  37.     'Required by the Windows Form Designer
  38.     Private components As System.ComponentModel.Container
  39.  
  40.     'NOTE: The following procedure is required by the Windows Form Designer
  41.     'It can be modified using the Windows Form Designer.  
  42.     'Do not modify it using the code editor.
  43.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  44.         Me.chkEnableEvents = New System.Windows.Forms.CheckBox()
  45.         Me.Label1 = New System.Windows.Forms.Label()
  46.         Me.Label2 = New System.Windows.Forms.Label()
  47.         Me.txtPath = New System.Windows.Forms.TextBox()
  48.         Me.txtFilter = New System.Windows.Forms.TextBox()
  49.         Me.txtLog = New System.Windows.Forms.TextBox()
  50.         Me.btnWaitForChanged = New System.Windows.Forms.Button()
  51.         Me.fsw = New System.IO.FileSystemWatcher()
  52.         Me.chkIncludeSubdirs = New System.Windows.Forms.CheckBox()
  53.         CType(Me.fsw, System.ComponentModel.ISupportInitialize).BeginInit()
  54.         Me.SuspendLayout()
  55.         '
  56.         'chkEnableEvents
  57.         '
  58.         Me.chkEnableEvents.Location = New System.Drawing.Point(16, 136)
  59.         Me.chkEnableEvents.Name = "chkEnableEvents"
  60.         Me.chkEnableEvents.Size = New System.Drawing.Size(216, 16)
  61.         Me.chkEnableEvents.TabIndex = 6
  62.         Me.chkEnableEvents.Text = "Enable Events"
  63.         '
  64.         'Label1
  65.         '
  66.         Me.Label1.Location = New System.Drawing.Point(16, 16)
  67.         Me.Label1.Name = "Label1"
  68.         Me.Label1.Size = New System.Drawing.Size(248, 16)
  69.         Me.Label1.TabIndex = 0
  70.         Me.Label1.Text = "Path"
  71.         '
  72.         'Label2
  73.         '
  74.         Me.Label2.Location = New System.Drawing.Point(16, 72)
  75.         Me.Label2.Name = "Label2"
  76.         Me.Label2.Size = New System.Drawing.Size(248, 16)
  77.         Me.Label2.TabIndex = 0
  78.         Me.Label2.Text = "Filter"
  79.         '
  80.         'txtPath
  81.         '
  82.         Me.txtPath.Location = New System.Drawing.Point(16, 40)
  83.         Me.txtPath.Name = "txtPath"
  84.         Me.txtPath.Size = New System.Drawing.Size(416, 24)
  85.         Me.txtPath.TabIndex = 1
  86.         Me.txtPath.Text = "c:\"
  87.         '
  88.         'txtFilter
  89.         '
  90.         Me.txtFilter.Location = New System.Drawing.Point(16, 96)
  91.         Me.txtFilter.Name = "txtFilter"
  92.         Me.txtFilter.Size = New System.Drawing.Size(176, 24)
  93.         Me.txtFilter.TabIndex = 1
  94.         Me.txtFilter.Text = "*.*"
  95.         '
  96.         'txtLog
  97.         '
  98.         Me.txtLog.Location = New System.Drawing.Point(16, 160)
  99.         Me.txtLog.Multiline = True
  100.         Me.txtLog.Name = "txtLog"
  101.         Me.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Both
  102.         Me.txtLog.Size = New System.Drawing.Size(424, 104)
  103.         Me.txtLog.TabIndex = 5
  104.         Me.txtLog.Text = ""
  105.         '
  106.         'btnWaitForChanged
  107.         '
  108.         Me.btnWaitForChanged.Location = New System.Drawing.Point(232, 128)
  109.         Me.btnWaitForChanged.Name = "btnWaitForChanged"
  110.         Me.btnWaitForChanged.Size = New System.Drawing.Size(176, 24)
  111.         Me.btnWaitForChanged.TabIndex = 7
  112.         Me.btnWaitForChanged.Text = "WaitForChanged"
  113.         '
  114.         'fsw
  115.         '
  116.         Me.fsw.NotifyFilter = ((System.IO.NotifyFilters.FileName Or System.IO.NotifyFilters.DirectoryName) _
  117.                     Or System.IO.NotifyFilters.LastWrite)
  118.         Me.fsw.Path = "c:\"
  119.         Me.fsw.SynchronizingObject = Me
  120.         '
  121.         'chkIncludeSubdirs
  122.         '
  123.         Me.chkIncludeSubdirs.Location = New System.Drawing.Point(224, 96)
  124.         Me.chkIncludeSubdirs.Name = "chkIncludeSubdirs"
  125.         Me.chkIncludeSubdirs.Size = New System.Drawing.Size(176, 16)
  126.         Me.chkIncludeSubdirs.TabIndex = 4
  127.         Me.chkIncludeSubdirs.Text = "Include Subdirectories"
  128.         '
  129.         'FileSystemWatcherForm
  130.         '
  131.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  132.         Me.ClientSize = New System.Drawing.Size(448, 277)
  133.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWaitForChanged, Me.chkEnableEvents, Me.txtLog, Me.chkIncludeSubdirs, Me.txtFilter, Me.Label2, Me.txtPath, Me.Label1})
  134.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  135.         Me.Name = "FileSystemWatcherForm"
  136.         Me.Text = "FileSystemWatcherForm"
  137.         CType(Me.fsw, System.ComponentModel.ISupportInitialize).EndInit()
  138.         Me.ResumeLayout(False)
  139.  
  140.     End Sub
  141.  
  142. #End Region
  143.  
  144. #Const USE_SINGLE_EVENT_HANDLER = False
  145.  
  146. #If USE_SINGLE_EVENT_HANDLER Then
  147.         ' You can use one event handler for all three events
  148.     Private Sub fsw_All(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Changed, fsw.Created, fsw.Deleted
  149.         Dim changeType As String = [Enum].GetName(GetType(io.WatcherChangeTypes), e.ChangeType)
  150.         LogMessage("File changed: " & e.FullPath & " (" & changeType & ")")
  151.     End Sub
  152.  
  153. #Else
  154.     ' three separate event handlers
  155.  
  156.     Private Sub fsw_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Changed
  157.         LogMessage("File changed: " & e.FullPath)
  158.     End Sub
  159.  
  160.     Private Sub fsw_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Created
  161.         LogMessage("File created: " & e.FullPath)
  162.     End Sub
  163.  
  164.     Private Sub fsw_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Deleted
  165.         LogMessage("File deleted: " & e.FullPath)
  166.     End Sub
  167. #End If
  168.  
  169.     ' two more event handlers
  170.  
  171.     Private Sub fsw_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles fsw.Renamed
  172.         LogMessage("File renamed: " & e.OldFullPath & " => " & e.FullPath)
  173.     End Sub
  174.  
  175.     Private Sub fsw_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs) Handles fsw.Error
  176.         LogMessage("FileSystemWatcher error")
  177.     End Sub
  178.  
  179.     ' Add a string to the log
  180.     Sub LogMessage(ByVal msg As String)
  181.         txtLog.AppendText(msg & ControlChars.CrLf)
  182.     End Sub
  183.  
  184.     ' enable/disable events
  185.  
  186.     Private Sub chkEnableEvents_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEnableEvents.CheckedChanged
  187.         Dim f As New System.IO.FileSystemWatcher()
  188.  
  189.         If chkEnableEvents.Checked Then
  190.             fsw.Path = txtPath.Text
  191.             fsw.Filter = txtFilter.Text
  192.             fsw.IncludeSubdirectories = chkIncludeSubdirs.Checked
  193.             fsw.SynchronizingObject = Me
  194.             fsw.NotifyFilter = NotifyFilters.Attributes Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
  195.             fsw.EnableRaisingEvents = True
  196.         Else
  197.             fsw.EnableRaisingEvents = False
  198.         End If
  199.     End Sub
  200.  
  201.     ' demonstrate the WaitForChanged method
  202.  
  203.     Private Sub btnWaitForChanged_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWaitForChanged.Click
  204.         ' Create a *new* FileSystemWatcher component.
  205.         Dim tmpFsw As New FileSystemWatcher(txtPath.Text, txtFilter.Text)
  206.         ' Wait max 10 seconds for an event.
  207.         Dim res As WaitForChangedResult = tmpFsw.WaitForChanged(WatcherChangeTypes.All, 10000)
  208.  
  209.         If res.TimedOut Then
  210.             LogMessage("10 seconds have elapsed without any event")
  211.         Else
  212.             Dim changeType As String = [Enum].GetName(GetType(IO.WatcherChangeTypes), res.ChangeType)
  213.             LogMessage("Event: " & res.Name & " (" & changeType & ")")
  214.         End If
  215.     End Sub
  216. End Class
  217.